import { customForm } from '@/components/ProForm/interface/index';
import { User } from '@/api/interface';
import { ProTableValue } from '@/interface/public';

import { defaultStyle } from '@/basic/style';

export function useAddForm(proTableBox: ProTableValue) {
  const userStore = useUserStore();

  let addForm = reactive<customForm<User.ResUserList>>({
    form: {},
    title: '',
    visible: false,
    options: [
      {
        label: 'id',
        prop: 'id',
        hide: true,
        type: 'input',
        defaultValue: ''
      },

      {
        type: 'input',
        label: 'username',
        hide: false,
        prop: 'username',
        defaultValue: '',
        attrs: {
          disabled: false
        },
        rules: [{ required: true, message: '请输入账号', trigger: 'blur' }]
      },
      {
        type: 'input',
        label: '名称',
        hide: false,
        defaultValue: '',
        prop: 'nick',

        rules: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      },
      {
        type: 'input',
        label: 'password',
        hide: false,
        defaultValue: '',
        prop: 'password',
        attrs: {
          'show-password': true
        },
        rules: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      {
        type: 'input',
        label: 'remark',
        defaultValue: '',
        hide: false,
        prop: 'remark',
        attrs: {
          style: defaultStyle
        }
      }
    ]
  });

  const saveAddForm = async form => {
    const params = { ...form };
    const { customerName } = params;
    if (customerName) {
      const c = customerName.split(',');
      params.customerName = c[1];
      params.customerId = c[0];
    }

    form.id ? await userUpdate(form) : await userSave(params);
    addForm.visible = false;
    elMessage();

    proTableBox.value?.getTableList();
  };

  return { addForm, saveAddForm };
}
